package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.*;
import com.example.jyjava.dao.ShopDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopDao shopDao;

    //商品查询
    @RequestMapping("/getShop")
    @ResponseBody
    public String getShop(String shopType) {
        String falg = "400";
//        System.out.println(shopType);
        List<Shop> shops = shopDao.getAllShop(shopType);
        HashMap<String, Object> res = new HashMap<>();  //map封装
//        System.out.println(shops);
        if (shops != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopList", shops);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    //模糊查询
    @RequestMapping("/getSign")
    @ResponseBody
    public String getSign(QueryInfo queryInfo) {
        String falg = "400";
        List<Shop> shops = shopDao.getShopSign("%" + queryInfo.getQuery() + "%");
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (shops != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopList", shops);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    //根据id查询商品
    @RequestMapping("/getShopSign")
    @ResponseBody
    public String getShopQx(Integer id) {
        String falg = "400";
        Shop shop = shopDao.getShopQx(id);
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (shop != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopSing", shop);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    //根据id添加商品
    @RequestMapping("/addShop")
    @ResponseBody
    public String addShop(@RequestBody Shop shop) {
        String falg = "400";
        shop.setShopBuy("0");
        shop.setShopState(true);
        shop.setUpdateTime(new Date());
        shop.setCreatTime(new Date());
        shop.setShopSold(false);
        shop.setShopLook("0");
        shop.setDeleteType(false);
        int i = shopDao.addShop(shop);
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (i > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopSing", i);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/addShopLook")
    @ResponseBody
    public String addShopLook(@RequestBody Shop shop) {
        String falg = "400";
//        System.out.println(id);
        int b = shopDao.updateShopLook(shop.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (b > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("shop", b);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    //根据用户查询商品
    @RequestMapping("/getAllShopT")
    @ResponseBody
    public String getAllShopT(@RequestBody Shop shop) {
        String falg = "400";
        List<Shop> shops = shopDao.getAllShopT(shop.getUserId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (shops != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shops", shops);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/updateShopSold")
    public String updateShopSold(@RequestBody Shop shop) {
        String falg = "400";
//        System.out.println(id);
        int b = shopDao.updateShopSold(shop.getId(), shop.isShopSold());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (b > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("shop", b);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }


    //根据id修改商品
    @RequestMapping("/updateShop")
    @ResponseBody
    public String updateShop(@RequestBody Shop shop) {
        String falg = "400";
        shop.setUpdateTime(new Date());
        shop.setCreatTime(new Date());
        shop.setShopSold(false);
        shop.setShopLook("0");
        shop.setDeleteType(false);
        int i = shopDao.addShop(shop);
        int b = shopDao.deleteShopSold(shop.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装

        if (i > 0 && b > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopSing", i);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    //根据id删除商品
    @RequestMapping("/deleteShop")
    @ResponseBody
    public String deleteShop(@RequestBody Shop shop) {
        String falg = "400";
        int b = shopDao.deleteShopSold(shop.getId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
        if (b > 0) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopSing", b);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }


    @RequestMapping("/getShopList")
    public String getShopList(QueryInfo queryInfo) {
        String falg = "400";
        //获取最大列表和当前编号
        int numbers = shopDao.getShopCounts("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
        List<Shop> shopList = shopDao.getShopList("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();

        if (shopList != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("number", numbers);
            res.put("shopList", shopList);
        } else {
            res.put("flag", falg);
        }
        String res_string = JSON.toJSONString(res);
        return res_string;
    }


    //商品查询
    @RequestMapping("/getShopType")
    @ResponseBody
    public String getShop(TypeQueryInfo typeQueryInfo) {
        String falg = "400";
        List<Shop> shops = shopDao.getShopType("%" + typeQueryInfo.getQuery() + "%", typeQueryInfo.getShopType());
        HashMap<String, Object> res = new HashMap<>();  //map封装
//        System.out.println(shops);
        if (shops != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopList", shops);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }


    //商品查询
    @RequestMapping("/getShopUser")
    @ResponseBody
    public String getShopUser(ShopQueryInfo shopQueryInfo) {
        String falg = "400";
        int numbers = shopDao.getShopUserC("%" + shopQueryInfo.getQuery() + "%",shopQueryInfo.getUserId());
        int pageStart = (shopQueryInfo.getPageNum() - 1) * shopQueryInfo.getPageSize();
        List<Shop> shops = shopDao.getShopUser("%" + shopQueryInfo.getQuery() + "%", pageStart, shopQueryInfo.getPageSize(), shopQueryInfo.getUserId());
        HashMap<String, Object> res = new HashMap<>();  //map封装
//        System.out.println(shops);
        if (shops != null) {
            falg = "200";
            res.put("flag", falg);
            res.put("shopList", shops);
            res.put("number", numbers);
        } else {
            res.put("flag", falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
